import os
from datetime import datetime

class log_helper():
    log_path: str
    console = False
    def __init__(self, logpath = "C:\\Logs\\tqsdk\\", console = False):
        self.log_path = logpath
        self.console = console
    
    def log_file(self, content: str,
                    filename=None):
        try:
            if not os.path.exists(self.log_path):
                os.makedirs(self.log_path)

            if filename is None:
                filename = f"{datetime.now().strftime('%Y%m')}.log"

            file = os.path.join(self.log_path, filename)

            with open(file, 'a') as file_obj:
                file_obj.writelines(
                    f"{datetime.now()}  {content}\n"
                    "")
        except Exception as e:
            print(e)

    def log_life(self, content: str):
        if self.console:
            print(content)

        filename = f"{datetime.now().strftime('%Y%m')}_Life.log"
        self.log_file(content=content, filename=filename)

    def log_trade(self, content: str):
        if self.console:
            print(content)

        filename = f"{datetime.now().strftime('%Y%m')}_Trade.log"
        self.log_file(content=content, filename=filename)

    def log_error(self,  content: str):
        if self.console:
            print(content)

        filename = f"{datetime.now().strftime('%Y%m%d')}_Error.log"
        self.log_file(content=content, filename=filename)
        